﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BOSS.Framework.Search.UrlBuilder
{
    /// <summary>
    /// 标准的 URL 构造器实现
    /// </summary>
    public class StandardUrlBuilder : AbstractUrlBuilder
    {
        /// <summary>
        /// 构造器名称
        /// </summary>
        public const string BUILDER_NAME = "STD";

        /// <summary>
        /// 缺省 URL 格式
        /// </summary>
        public const string DEFAULT_URL_FORMAT = "searchResult";

        /// <summary>
        /// 缓存
        /// </summary>
        protected StringBuilder m_url = new StringBuilder();

        private int m_filtersCount;
        private int m_sortsCount;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="urlFormat"></param>
        /// <param name="userData"></param>
        public StandardUrlBuilder(string urlFormat, object userData)
            : base(urlFormat, userData)
        {
        }

        #region IUrlBuilder Members

        /// <summary>
        /// 获取 构造器名称
        /// </summary>
        public override string Name
        {
            get { return BUILDER_NAME; }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="contentType"></param>
        /// <remarks>{0}/search/{1}/, url, contentType</remarks>
        public override void Initialize(string contentType)
        {
            this.m_url.Length = 0;
            this.m_filtersCount = 0;
            this.m_sortsCount = 0;

            switch (this.UrlFormat)
            {
                case DEFAULT_URL_FORMAT:
                default:
                    this.m_url.Append("/search/").Append(contentType).Append('/');
                    break;
            }
        }

        /// <summary>
        /// 开始设置过滤参数
        /// </summary>
        public override void BeginFilter()
        {
            this.m_filtersCount = 0;
        }

        /// <summary>
        /// 设置 过滤参数
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public override void SetFilter(string fieldName, int value)
        {
            this.SetFilter(fieldName, (decimal)value);
        }

        /// <summary>
        /// 设置 过滤参数
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public override void SetFilter(string fieldName, decimal value)
        {
            this.m_url.Append(fieldName).Append('!').Append(value).Append(';');
            this.m_filtersCount++;
        }

        /// <summary>
        /// 设置 过滤参数
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public override void SetFilter(string fieldName, string value)
        {
            this.m_url.Append(fieldName).Append('!').Append(HttpUtility.UrlEncode(value)).Append(';');
            this.m_filtersCount++;
        }

        /// <summary>
        /// 设置 过滤参数
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="minValue"></param>
        /// <param name="includeMinValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="includeMaxValue"></param>
        public override void SetFilter(string fieldName, int minValue, bool includeMinValue, int maxValue, bool includeMaxValue)
        {
            this.SetFilter(fieldName, (decimal)minValue, includeMinValue, (decimal)maxValue, includeMaxValue);
        }

        /// <summary>
        /// 设置 过滤参数
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="minValue"></param>
        /// <param name="includeMinValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="includeMaxValue"></param>
        public override void SetFilter(string fieldName, decimal minValue, bool includeMinValue, decimal maxValue, bool includeMaxValue)
        {
            this.m_url.Append(fieldName).Append('!');
            if (includeMinValue)
            {
                this.m_url.Append('[');
            }
            else
            {
                this.m_url.Append('{');
            }
            this.m_url.Append(minValue)
                .Append('-')
                .Append(maxValue);
            if (includeMaxValue)
            {
                this.m_url.Append(']');
            }
            else
            {
                this.m_url.Append('}');
            }
            this.m_url.Append(';');
            this.m_filtersCount++;
        }

        /// <summary>
        /// 设置 过滤参数
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="minValue"></param>
        /// <param name="includeMinValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="includeMaxValue"></param>
        public override void SetFilter(string fieldName, DateTime minValue, bool includeMinValue, DateTime maxValue, bool includeMaxValue)
        {
            this.m_url.Append(fieldName).Append('!');
            if (includeMinValue)
            {
                this.m_url.Append('[');
            }
            else
            {
                this.m_url.Append('{');
            }
            this.m_url.Append(minValue.ToString("yyyyMMdd"))
                .Append('-')
                .Append(maxValue.ToString("yyyyMMdd"));
            if (includeMaxValue)
            {
                this.m_url.Append(']');
            }
            else
            {
                this.m_url.Append('}');
            }
            this.m_url.Append(';');
            this.m_filtersCount++;
        }

        /// <summary>
        /// 完成过滤参数的设置
        /// </summary>
        public override void EndFilter()
        {
            if (this.m_filtersCount == 0)
            {
                this.m_url.Append('-');
            }
            else
            {
                this.m_url.Length--;
            }
            this.m_url.Append('/');
        }

        /// <summary>
        /// 开始设置排序参数
        /// </summary>
        public override void BeginSort()
        {
            this.m_sortsCount = 0;
        }

        /// <summary>
        /// 设置 排序参数
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="descend"></param>
        public override void SetSort(string fieldName, bool descend)
        {
            if (descend)
            {
                this.m_url.Append('!');
            }
            this.m_url.Append(fieldName).Append(';');
            this.m_sortsCount++;
        }

        /// <summary>
        /// 完成排序参数的设置
        /// </summary>
        public override void EndSort()
        {
            if (this.m_sortsCount == 0)
            {
                this.m_url.Append('-');
            }
            else
            {
                this.m_url.Length--;
            }
            this.m_url.Append('/');
        }

        /// <summary>
        /// 设置 分页大小
        /// </summary>
        /// <param name="pageSize"></param>
        public override void SetPageSize(int pageSize)
        {
            this.m_url.Append(pageSize).Append('/');
        }

        /// <summary>
        /// 获取 URL
        /// </summary>
        /// <param name="pageIndex">当前页序号</param>
        /// <returns></returns>
        public override string GetUrl(int pageIndex)
        {
            return string.Format("{0}{1}.htm", this.m_url, pageIndex);
        }

        #endregion
    }
}
